package mrtran.iti.vn;

import java.lang.Thread.State;
import java.util.ArrayList;
import java.util.List;

import tuanvn.iti.vn.SpriteHP;
import tuanvn.iti.vn.SpriteItem;
import tuanvn.iti.vn.SpriteKimCuong;
import tuanvn.iti.vn.SpriteKimCuong2;
import tuanvn.iti.vn.SpriteKimCuong3;
import tuanvn.iti.vn.SpriteLuffy;
import tuanvn.iti.vn.SpriteMonster1;
import tuanvn.iti.vn.SpriteMonster2;
import tuanvn.iti.vn.SpriteMonster3;
import tuanvn.iti.vn.SpriteMonster4;
import tuanvn.iti.vn.SpriteMonster5;
import tuanvn.iti.vn.SpriteTemp;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Display;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import dubk.iti.vn.GameLoopThread;
import dubk.iti.vn.Music;
import dubk.iti.vn.Prefs;
import dubk.iti.vn.define;

public class Main extends Activity implements OnTouchListener, define {
	private Bitmap bmpBlood, bmpkimcuong,bmpkimcuong2,bmpkimcuong3, bmpHp, monster1, monster2,bmpMonster5,
			bmpMonster3,bmpMonster4,bmpItem;
	private Bitmap luffy,back,forward,attack,backGround;
	 public static GameView gV;
	float xBack, yBack, xForward, yForward, xAttack, yAttack, moveBackGround;
	public static int jewel = 0,item=0;
	public static Paint text,mHP,mLose,mWin;
	
	private List<SpriteHP> spriteHP = new ArrayList<SpriteHP>();
	private List<SpriteItem> spriteItems = new ArrayList<SpriteItem>();
	private List<SpriteKimCuong> spriteKimCuong = new ArrayList<SpriteKimCuong>();
	private List<SpriteKimCuong2> spriteKimCuong2 = new ArrayList<SpriteKimCuong2>();
	private List<SpriteKimCuong3> spriteKimCuong3 = new ArrayList<SpriteKimCuong3>();
	private List<SpriteLuffy> spriteLuffy = new ArrayList<SpriteLuffy>();
	private List<SpriteMonster1> spriteMonster1 = new ArrayList<SpriteMonster1>();
	private List<SpriteMonster2> spriteMonster2 = new ArrayList<SpriteMonster2>();
	private List<SpriteMonster3> spriteMonster3 = new ArrayList<SpriteMonster3>();
	private List<SpriteMonster4> spriteMonster4 = new ArrayList<SpriteMonster4>();
	private List<SpriteMonster5> spriteMonster5 = new ArrayList<SpriteMonster5>();
	private List<SpriteTemp> temps = new ArrayList<SpriteTemp>();
	
    public static int  SCREENWIDTH;
    public static int SCREENHEIGHT;
    public static boolean finish=true;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		gV = new GameView(this);
		gV.setOnTouchListener(this);
		setContentView(gV);
		 finish=true;
	       Display display = ((WindowManager)getBaseContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
	        SCREENWIDTH  = display.getWidth();
	        SCREENHEIGHT = display.getHeight();
	         	        
	}

	public class GameView extends SurfaceView {
		private SurfaceHolder holder;
		private GameLoopThread gameLoopThread;

		public GameView(Context context) {
			super(context);
			
			text = new Paint();
			text.setColor(Color.GREEN);
			text.setTextSize(20);
			text.setTypeface(Typeface.DEFAULT_BOLD);
			
			mLose = new Paint();
			mLose.setColor(Color.GRAY);
			mLose.setTextSize(60);
			mLose.setTypeface(Typeface.DEFAULT_BOLD);
			
			mWin = new Paint();
			mWin.setColor(Color.RED);
			mWin.setTextSize(60);
			mWin.setTypeface(Typeface.DEFAULT_BOLD);
			
			mHP = new Paint();			
			mHP.setColor(Color.RED);
			mHP.setTypeface(Typeface.DEFAULT_BOLD);
			mHP.setShader(null);
			mHP.setStyle(Paint.Style.FILL);
			mHP.setStrokeWidth(25);
			
			gameLoopThread = new GameLoopThread(this);
			holder = getHolder();
			holder.addCallback(new Callback() {
				public void surfaceDestroyed(SurfaceHolder arg0) {
					boolean retry = true;
					gameLoopThread.setRunning(false);
					Music.stop(getContext());
					while (retry) {
						try {
							gameLoopThread.join();
							retry = false;
						} catch (InterruptedException e) {
						}
					}
				}
				public void surfaceCreated(SurfaceHolder arg0) {
					createSprites();
					createSprites2();
					createSprites3();
					createSprites5();
					createSpritesHP();
					createSpritesLuffy();
				    Music.play(getBaseContext(), R.raw.soundtrack);
					gameLoopThread.setRunning(true);
					gameLoopThread.start();
				}
				public void surfaceChanged(SurfaceHolder arg0, int arg1,
						int arg2, int arg3) {
				}
			});
			bmpBlood = BitmapFactory.decodeResource(getResources(),
					R.drawable.blood);
			bmpItem = BitmapFactory.decodeResource(getResources(),
					R.drawable.meat);
			bmpkimcuong = BitmapFactory.decodeResource(getResources(),
					R.drawable.jewel);
			bmpkimcuong2 = BitmapFactory.decodeResource(getResources(),
					R.drawable.jewel);
			bmpkimcuong3 = BitmapFactory.decodeResource(getResources(),
					R.drawable.jewel);
			back = BitmapFactory
					.decodeResource(getResources(), R.drawable.back);
			forward = BitmapFactory.decodeResource(getResources(),
					R.drawable.forward);
			attack = BitmapFactory.decodeResource(getResources(),
					R.drawable.attack);
			backGround = BitmapFactory.decodeResource(getResources(),
					R.drawable.background);
		
			bmpMonster4=BitmapFactory.decodeResource(getResources(), R.drawable.ivankov);
		
		}

	private void createSpritesHP() {
			spriteHP.add(createSpriteHP(R.drawable.hpluffy));
	}
		private void createSpritesLuffy() {
			spriteLuffy.add(createSpriteLuffy(R.drawable.luffyanimation));
		}
		private void createSprites() {
			spriteMonster1.add(createSprite(R.drawable.morgan));
		}

		private void createSprites2() {
			spriteMonster2.add(createSprite2(R.drawable.duval));
		}
		private void createSprites3() {
			spriteMonster3.add(createSprite3(R.drawable.wapol));
		}
		private void createSprites5() {
			spriteMonster5.add(createSprite5(R.drawable.blackbeard));
		}
		
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

		private SpriteHP createSpriteHP(int resouce) {
			bmpHp = BitmapFactory.decodeResource(getResources(), resouce);
			return new SpriteHP(this, bmpHp, spriteHP);
		}
		private SpriteLuffy createSpriteLuffy(int resouce) {
		luffy = BitmapFactory.decodeResource(getResources(), resouce);
			return new SpriteLuffy(this, luffy, spriteLuffy);
		}
		private SpriteMonster1 createSprite(int resouce) {
			monster1 = BitmapFactory.decodeResource(getResources(), resouce);
			return new SpriteMonster1(this, monster1, spriteMonster1);
		}

		private SpriteMonster2 createSprite2(int resouce2) {
			monster2 = BitmapFactory.decodeResource(getResources(), resouce2);
			return new SpriteMonster2(this, monster2, spriteMonster2);
		}
		private SpriteMonster3 createSprite3(int resouce3) {
			bmpMonster3 = BitmapFactory.decodeResource(getResources(), resouce3);
			return new SpriteMonster3(this, bmpMonster3, spriteMonster3);
		}
		private SpriteMonster5 createSprite5(int resouce5) {
			bmpMonster5 = BitmapFactory.decodeResource(getResources(), resouce5);
			return new SpriteMonster5(this, bmpMonster5, spriteMonster5);
		}
		

		// ---------------------------------------------------------XU LY HAM ON
		// DRAW-------------------------------------------------------------------
		// --------------------------------------------------------------------------------------------------------------------------------------------
		@Override
		public void onDraw(Canvas canvas) {
			moveBackGround = moveBackGround - 2;
			float newFarYisl = (moveBackGround + backGround.getWidth());
			if (newFarYisl <= 0) {
				moveBackGround = 0;
				canvas.drawBitmap(backGround, moveBackGround, 0, null);
			} else {
				canvas.drawBitmap(backGround, moveBackGround, 0, null);
				canvas.drawBitmap(backGround, +newFarYisl, 0, null);
			}
			
			for (int z = spriteItems.size() - 1; z >= 0; z--) {
				spriteItems.get(z).onDraw(canvas);
			}	
			for (int i = temps.size() - 1; i >= 0; i--) {
				temps.get(i).onDraw(canvas);
			}
			for (int j = spriteKimCuong.size() - 1; j >= 0; j--) {
				spriteKimCuong.get(j).onDraw(canvas);
			}
			for (int x = spriteKimCuong2.size() - 1; x >= 0; x--) {
				spriteKimCuong2.get(x).onDraw(canvas);
			}
			for (int y = spriteKimCuong3.size() - 1; y >= 0; y--) {
				spriteKimCuong3.get(y).onDraw(canvas);
			}
		
			for (SpriteMonster1 sprite : spriteMonster1) {
				sprite.onDraw(canvas);
			}
			for (SpriteMonster2 sprite2 : spriteMonster2) {
				sprite2.onDraw(canvas);
			}
			for (SpriteMonster3 sprite3 : spriteMonster3) {
				sprite3.onDraw(canvas);
			}
			for (SpriteMonster4 sprite4 : spriteMonster4) {
				sprite4.onDraw(canvas);
			}
			for (SpriteMonster5 sprite5 : spriteMonster5) {
				sprite5.onDraw(canvas);
			}

			for (SpriteHP spriteHp : spriteHP) {
				spriteHp.onDraw(canvas);
			}
			for (SpriteLuffy spriteluffy : spriteLuffy) {
				spriteluffy.onDraw(canvas);
			}
			
			
			canvas.drawBitmap(back,SCREENWIDTH/30,SCREENHEIGHT/1.7F+back.getHeight(), null);
			canvas.drawBitmap(forward, SCREENWIDTH/25+back.getWidth(),SCREENHEIGHT/1.7F+back.getHeight(), null);
			canvas.drawBitmap(attack, SCREENWIDTH/1.5F, SCREENHEIGHT/1.7F+back.getHeight(), null);
			canvas.drawText("Jewel: " + jewel + "/3",SCREENWIDTH/1.5F, SCREENHEIGHT/20, text);
		
		
			for(int i = 0; i < spriteMonster1.size(); i++){				
						if(SpriteMonster1.distance1 < SpriteMonster1.widthLuffyMonster1-SpriteLuffy.luffyWidth){			
								SpriteHP.mlife=SpriteHP.mlife-5;							
								spriteMonster1.remove(i);
								break;
						}						
			}
			for(int j = 0; j < spriteMonster2.size(); j++){				
				if(SpriteMonster2.distance2 <SpriteMonster2.widthLuffyMonster2-SpriteLuffy.luffyWidth){			
						SpriteHP.mlife=SpriteHP.mlife-5;							
						spriteMonster2.remove(j);
						break;
				}						
			}
			for(int k = 0; k < spriteMonster3.size(); k++){				
				if(SpriteMonster3.distance3 <SpriteMonster3.widthLuffyMonster3-SpriteLuffy.luffyWidth){			
						SpriteHP.mlife=SpriteHP.mlife-5;							
						spriteMonster3.remove(k);
						break;
				}						
			}
			for(int o = 0; o < spriteMonster4.size(); o++){				
				if(SpriteMonster4.distance4 <SpriteMonster4.widthLuffyMonster4-SpriteLuffy.luffyWidth){			
						SpriteHP.mlife=SpriteHP.mlife-5;							
						spriteMonster4.remove(o);
						break;
				}						
			}
			for(int p = 0; p < spriteMonster5.size(); p++){				
				if(SpriteMonster5.distance5 <SpriteMonster5.widthLuffyMonster5-SpriteLuffy.luffyWidth){			
						SpriteHP.mlife=SpriteHP.mlife-5;							
						spriteMonster5.remove(p);
					
						break;
				}						
			}
		
			if(SpriteHP.mlife==20){
				canvas.drawLine(SCREENWIDTH/10, SCREENHEIGHT/9, SCREENWIDTH/3, SCREENHEIGHT/9, mHP);}
			else if(SpriteHP.mlife==15){
				canvas.drawLine(SCREENWIDTH/10, SCREENHEIGHT/9, SCREENWIDTH/4, SCREENHEIGHT/9, mHP);}
			else if(SpriteHP.mlife==10){
			canvas.drawLine(SCREENWIDTH/10, SCREENHEIGHT/9, SCREENWIDTH/5, SCREENHEIGHT/9, mHP);
			}else if(SpriteHP.mlife==5){
				canvas.drawLine(SCREENWIDTH/10, SCREENHEIGHT/9, SCREENWIDTH/6, SCREENHEIGHT/9, mHP);
			}else{
				canvas.drawText("YOU LOSE " ,SCREENWIDTH/3, SCREENHEIGHT/2, mLose);
				for(int z = 0; z < spriteLuffy.size(); z++){																	
							spriteLuffy.remove(z);
							break;
					}					
				}
			if(jewel==3) canvas.drawText("YOU WIN " ,SCREENWIDTH/3, SCREENHEIGHT/2, mWin);
				
			
			super.onDraw(canvas);
		}
		 public void resume() { 
		       GameLoopThread.running = true;
		       gameLoopThread = new GameLoopThread(this);
		       gameLoopThread.start();   
		 
		    }      

		 public void pause() {                        
			 GameLoopThread.running  = false;                        
		        while(true) {
		            try {
		            	gameLoopThread.join();
		                break;
		            } catch (InterruptedException e) {	               
		            }}
		        }   
	 
			protected void onDestroy() {
				GameLoopThread.play=false;
				finish = false;
			    GameLoopThread.running= false;
			    Music.stop(getContext());
			    boolean retry = true;
				while (retry) {
				     try {
				    	 if(gameLoopThread.getState()==State.BLOCKED)
				    		 gameLoopThread.join();
				     retry = false;
				         } 
				     catch (InterruptedException e) {	
				     			}
							}	
					
			}
	}
	// ------------------------------------------------------XU LY HAM
	// ONTOUCH----------------------------------------------------------------------
	// --------------------------------------------------------------------------------------------------------------------------------------------
	public boolean onTouch(View v, MotionEvent event) {
		xBack = event.getX();
		yBack = event.getY();
		xForward = event.getX();
		yForward = event.getY();
		xAttack = event.getX();
		yAttack = event.getY();

		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			if ((xBack >= SCREENWIDTH/30 && xBack <= SCREENWIDTH/30+back.getWidth()) && (yBack >= SCREENHEIGHT/1.7F+back.getHeight() && yBack <= SCREENHEIGHT/1.7F+back.getHeight()+back.getHeight())) {
				SpriteLuffy.xLuffy -= 20;
				SpriteLuffy.ySpeed = 0;
				SpriteLuffy.xSpeed = -1;
				SpriteLuffy.currentFrame = ++SpriteLuffy.currentFrame
						% SpriteLuffy.BMP_COLUMNS;
			}

			if ((xForward >=SCREENWIDTH/25+back.getWidth() && xForward <= SCREENWIDTH/25+back.getWidth()+forward.getWidth())
					&& (yForward >= SCREENHEIGHT/1.7F+back.getHeight() && yForward <= SCREENHEIGHT/1.7F+back.getHeight()+forward.getHeight())) {			
					SpriteLuffy.xLuffy += 20;
					SpriteLuffy.ySpeed = 0;
					SpriteLuffy.xSpeed = 1;
					SpriteLuffy.currentFrame = ++SpriteLuffy.currentFrame
							% SpriteLuffy.BMP_COLUMNS;					
			}
			
			if ((xAttack >= SCREENWIDTH/1.5F && xAttack <= SCREENWIDTH/1.5F+attack.getWidth())
					&& (yAttack >= SCREENHEIGHT/1.7F+back.getHeight() && yAttack <=  SCREENHEIGHT/1.7F+back.getHeight()+attack.getHeight())) {
				if (SpriteLuffy.xSpeed == 1) {
					SpriteLuffy.xSpeed = 0;
					SpriteLuffy.ySpeed = 1;
				}
				if (SpriteLuffy.xSpeed == -1) {
					SpriteLuffy.xSpeed = 0;
					SpriteLuffy.ySpeed = -1;
				}
				SpriteLuffy.currentFrame = ++SpriteLuffy.currentFrame
						% SpriteLuffy.BMP_COLUMNS;

		
				
				if (SpriteMonster1.distance1 <= SpriteMonster1.widthLuffyMonster1) {
					for (int i = spriteMonster1.size() - 1; i >= 0; i--) {
						int xItem = SpriteItem.xItem;
						int yItem = SpriteItem.yItem;
						float x = SpriteMonster1.xMonsterCenter1 - 15;
						float y = SpriteMonster1.yMonsterCenter1;							
						SpriteMonster1 sprites = spriteMonster1.get(i);						
						spriteMonster1.remove(sprites);
						temps.add(new SpriteTemp(temps, this, x, y, bmpBlood));					
						spriteItems.add(new SpriteItem(gV, bmpItem,
								spriteItems, xItem, yItem));	
						break;
						
					}					
				}

				if(SpriteMonster2.distance2 <= SpriteMonster2.widthLuffyMonster2) {
					for (int j = spriteMonster2.size() - 1; j >= 0; j--) {
						float x2 = SpriteMonster2.xMonsterCenter2 - 15;
						float y2 = SpriteMonster2.yMonsterCenter2;
						int xKimCuong = SpriteKimCuong.xKimCuong;
						int yKimCuong = SpriteKimCuong.yKimCuong;
						SpriteMonster2 sprites2 = spriteMonster2.get(j);
						spriteMonster2.remove(sprites2);
						temps.add(new SpriteTemp(temps, this, x2, y2, bmpBlood));				
						spriteKimCuong.add(new SpriteKimCuong(gV, bmpkimcuong,
								spriteKimCuong, xKimCuong, yKimCuong));
						break;
					}
				

				}
				if (SpriteMonster3.distance3 <= SpriteMonster3.widthLuffyMonster3) {
					for (int k = spriteMonster3.size() - 1; k >= 0; k--) {
						int xKimCuong2 = SpriteKimCuong2.xKimCuong2;
						int yKimCuong2 = SpriteKimCuong2.yKimCuong2;
					float x3 = SpriteMonster3.xMonsterCenter3 - 15;
					float y3= SpriteMonster3.yMonsterCenter3;
						SpriteMonster3 sprites3 = spriteMonster3.get(k);						
						spriteMonster3.remove(sprites3);
						temps.add(new SpriteTemp(temps, this, x3, y3, bmpBlood));
					spriteMonster4.add(new SpriteMonster4(gV, bmpMonster4, spriteMonster4));
					spriteKimCuong2.add(new SpriteKimCuong2(gV, bmpkimcuong2,
							spriteKimCuong2, xKimCuong2, yKimCuong2));
						break;
						
					}
				}
				if (SpriteMonster4.distance4 <= SpriteMonster4.widthLuffyMonster4) {
					for (int l = spriteMonster4.size() - 1; l >= 0; l--) {
					float x4 = SpriteMonster4.xMonsterCenter4 - 15;
					float y4= SpriteMonster4.yMonsterCenter4;
						SpriteMonster4 sprites4 = spriteMonster4.get(l);						
						spriteMonster4.remove(sprites4);
						temps.add(new SpriteTemp(temps, this, x4, y4, bmpBlood));
						spriteMonster1.add(new SpriteMonster1(gV, monster1, spriteMonster1));
									
						break;
						
					}
				}
				if (SpriteMonster5.distance5 <= SpriteMonster5.widthLuffyMonster5) {
					for (int m = spriteMonster5.size() - 1; m >= 0; m--) {				
					float x5 = SpriteMonster5.xMonsterCenter5 - 15;
					float y5= SpriteMonster5.yMonsterCenter5;
						SpriteMonster5 sprites5 = spriteMonster5.get(m);						
						spriteMonster5.remove(sprites5);
						temps.add(new SpriteTemp(temps, this, x5, y5, bmpBlood));
						int xKimCuong3 = SpriteKimCuong.xKimCuong;
						int yKimCuong3 = SpriteKimCuong.yKimCuong;
						spriteKimCuong3.add(new SpriteKimCuong3(gV, bmpkimcuong3,
								spriteKimCuong3, xKimCuong3, yKimCuong3));
					
						break;
						
					}
				}
		
			}
		}
		return true;
	}
	private void AlertDialog() {
		AlertDialog.Builder mAlertDialog= new AlertDialog.Builder(Main.this);
		mAlertDialog.setTitle("Warning");
		mAlertDialog.setMessage("Pause");
		
		mAlertDialog.setPositiveButton("Continute", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				gV.resume();	  
				finish = true;
				
			}
		});
		
		mAlertDialog.setNegativeButton("Quit", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
		finish();
			}
		});
		mAlertDialog.show();
		
	}
	public   void AlertDialog2() {
		AlertDialog.Builder nAlertDialog= new AlertDialog.Builder(Main.this);
		
		nAlertDialog.setTitle("YOU WIN");
		nAlertDialog.setMessage("Do you want to continutes?");
		
		nAlertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				gV.resume();	  
				finish = true;
				
			}
		});
		
		nAlertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
		
			public void onClick(DialogInterface dialog, int which) {
		finish();
			}
		});
		nAlertDialog.show();
		
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
    		gV.pause();
    		finish=false;
    		AlertDialog();
    	    return true; 
        }
		return super.onKeyDown(keyCode, event);
	}

}